package com.example.demo13zuulinvoker.hystrix;

import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * @author Created by yawn on 2017-11-05 17:27
 */

@WebFilter(filterName = "hystrixFilter", urlPatterns = "/*")
public class HystrixFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HystrixRequestContext hrContext = HystrixRequestContext.initializeContext();
        try {
            System.out.println("============ hystrixFilter pre =============");
            chain.doFilter(request, response);
            System.out.println("============ hystrixFilter post ============");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            hrContext.shutdown();
        }
    }

    @Override
    public void destroy() {

    }
}
